﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace TextEditor
{
    
    /// <summary>
    /// 图片属性设置
    /// </summary>
    public class ImageTagProvider : SmartTagProvider
    {
        public override void GetCommands(ArrayList myList)
        {
            ZYTextImage currentImage = this.GetCurrentImage();
            if (currentImage != null)
            {
                myList.Add(new SmartTagItem(this, 4, "&R  设定名称[" + currentImage.ID + "]为..."));
                myList.Add(new SmartTagItem(this, 5, "&E  编辑图像..."));
                myList.Add(new SmartTagItem(this, 0, "&A  图片另存为..."));
                myList.Add(new SmartTagItem(this, 2, !currentImage.CanResize, "&L  锁定大小"));
                if (currentImage.CanResize)
                {
                    myList.Add(new SmartTagItem(this, 1, "&S  设置为原始大小"));
                    myList.Add(new SmartTagItem(this, 3, currentImage.ImageSizeRectLock, "&L  锁定宽度和高度的比例"));
                }
                myList.Add(new SmartTagItem("-"));
                myList.Add(new SmartTagItem("图片大小 " + ((currentImage.Image == null) ? "无图片" : string.Concat(new object[] { " 宽:", currentImage.Image.Size.Width, " 高:", currentImage.Image.Size.Height }))));
                myList.Add(new SmartTagItem(string.Concat(new object[] { "显示大小  宽 ", currentImage.Width, " 高:", currentImage.Height })));
                myList.Add(new SmartTagItem("水平分辨率 " + ((currentImage.Image == null) ? "无图片" : (currentImage.Image.HorizontalResolution + " 像素/英寸"))));
                myList.Add(new SmartTagItem("垂直分辨率 " + ((currentImage.Image == null) ? "无图片" : (currentImage.Image.VerticalResolution + " 像素/英寸"))));
            }
        }

        private ZYTextImage GetCurrentImage()
        {
            ZYTextImage currentElement = null;
            if ((base.OwnerDocument.Content.SelectLength == 0) && (base.OwnerDocument.Content.CurrentElement is ZYTextImage))
            {
                currentElement = base.OwnerDocument.Content.CurrentElement as ZYTextImage;
            }
            else if (base.OwnerDocument.Content.CurrentSelectElement is ZYTextImage)
            {
                currentElement = base.OwnerDocument.Content.CurrentSelectElement as ZYTextImage;
            }
            if ((currentElement != null) && !currentElement.Deleteted)
            {
                return currentElement;
            }
            return null;
        }

        public override Point GetPos()
        {
            ZYTextImage currentImage = this.GetCurrentImage();
            if (currentImage != null)
            {
                return new Point(currentImage.Bounds.Right + 2, currentImage.Bounds.Top + 2);
            }
            return Point.Empty;
        }

        public override bool HandleCommand(SmartTagItem item)
        {
            ZYTextImage currentImage = this.GetCurrentImage();
            if (currentImage != null)
            {
                Image image = currentImage.Image;
                switch (item.ID)
                {
                    case 0:
                        if (image == null)
                        {
                            break;
                        }
                        using (SaveFileDialog dialog = new SaveFileDialog())
                        {
                            dialog.OverwritePrompt = true;
                            dialog.CheckPathExists = true;
                            dialog.Filter = "PNG图片|*.png";
                            if (dialog.ShowDialog(base.OwnerControl) == DialogResult.OK)
                            {
                                image.Save(dialog.FileName, ImageFormat.Png);
                            }
                        }
                        return true;

                    case 1:
                        if (image == null)
                        {
                            break;
                        }
                        base.OwnerDocument.BeginContentChangeLog();
                        currentImage.Width = image.Size.Width;
                        currentImage.Height = image.Size.Height;
                        base.OwnerDocument.EndContentChangeLog();
                        base.OwnerDocument.RefreshLine();
                        base.OwnerDocument.UpdateView();
                        return true;

                    case 2:
                        currentImage.CanResize = !currentImage.CanResize;
                        base.OwnerDocument.RefreshElement(currentImage);
                        return true;

                    case 3:
                        currentImage.ImageSizeRectLock = !currentImage.ImageSizeRectLock;
                        if (currentImage.ImageSizeRectLock)
                        {
                            base.OwnerDocument.BeginContentChangeLog();
                            currentImage.Height = (int) (((double) currentImage.Width) / currentImage.WidthHeightRate);
                            base.OwnerDocument.EndContentChangeLog();
                            base.OwnerDocument.RefreshLine();
                            base.OwnerDocument.UpdateView();
                        }
                        return true;

                    case 4:
                    {
                        string str = dlgInputBox.InputBox("请输入图片新的编号", "修改编号", currentImage.ID);
                        if (str != null)
                        {
                            currentImage.ID = str;
                        }
                        return true;
                    }
                    case 5:
                        currentImage.EditImage();
                        return true;
                }
            }
            return false;
        }

        public override bool isEnable()
        {
            return (this.GetCurrentImage() != null);
        }

        public override int ImageIndex
        {
            get
            {
                return 15;
            }
        }
    }
}

